#define _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> mat(n, vector<int>(n));
        int top = 0, bottom = n - 1, left = 0, right = n - 1;
        int start = 1;
        while (start <= n * n)
        {
            //left->right
            for (int i = left; i <= right; i++)  mat[top][i] = start++;
            top++;
            //top->bottom
            for (int i = top; i <= bottom; i++)  mat[i][right] = start++;
            right--;
            //right->left
            for (int i = right; i >= left; i--) mat[bottom][i] = start++;
            bottom--;
            //bottom->top
            for (int i = bottom; i >= top; i--) mat[i][left] = start++;
            left++;
        }
        return mat;
    }
};